package com.apestech.framework.file.modul;

import java.util.ArrayList;

import com.apestech.framework.json.JSON;
import com.apestech.framework.json.JSONObject;

public class DictPageModul {
	
	private int count=0;
	private JSONObject dict=new JSONObject();
	private int fetchSize=100;
	private ArrayList<String> keylist;
	private int keycount;
	
	public DictPageModul(){
	}
	
	public DictPageModul(int fetchSize){
		this.fetchSize=fetchSize;
	}
	
	public void setFetchSize(int fetchSize){
		this.fetchSize=fetchSize;
	}
	
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	
	public JSONObject toJSON(){
		JSONObject map=new JSONObject();
		map.put("count", this.count);
		map.put("fetchSize", this.fetchSize);
		map.put("dict", dictToJSON());
		map.put("keylist", this.keylist);
		map.put("keycount", this.keycount);
		return map;
	}
	
	public void fromJSON(JSONObject map) throws Exception{
		if(map==null) return;
		this.count=(int) map.get("count");
		this.fetchSize=(int) map.get("fetchSize");
		this.keylist=(ArrayList<String>) map.get("keylist");
		this.keycount=(int) map.get("keycount");
		JSONObject pmap=map.getJSONObject("dict");
		dictFromJSON(pmap);
	}
	
	private JSONObject dictToJSON(){
		if(this.dict.size()==0) return null;
		JSONObject map=new JSONObject();
		int len=this.dict.size();
		for(int i=0; i<len; i++){
			String key=this.keylist.get(i);
			KeyPageModul page=(KeyPageModul) this.dict.get(key);
			map.put(key, page.toJSON());
		}
		return map;
	}
	
	private void dictFromJSON(JSONObject map) throws Exception{
		if(map==null) return;
		this.dict.clear();
		int len=map.size();
		ArrayList<String> list=map.getKeylist();
		for(int i=0; i<len; i++){
			String key=list.get(i);
			JSONObject pmap=map.getJSONObject(key);
			getKeyPage(key).fromJSON(pmap);
		}
	}
	
	private KeyPageModul getKeyPage(String key){
		KeyPageModul page=(KeyPageModul) this.dict.get(key);
		if(page==null){
			page=new KeyPageModul(key, this.fetchSize);
			this.dict.put(key, page);
			this.keylist=this.dict.getKeylist();
			this.keycount=this.dict.size();
		}
		return page;
	}
	
	public void addRow(String key){
		KeyPageModul page=getKeyPage(key);
		page.addRow();
		this.count++;
	}
	
	public void insRow(String key){
		KeyPageModul page=getKeyPage(key);
		page.insRow();
		this.count++;
	}
	
	public void removeRow(String key, int index){
		KeyPageModul page=getKeyPage(key);
		page.removeRow(index);
		this.count--;
	}
	
	public void addPage(String key, int row){
		KeyPageModul page=getKeyPage(key);
		page.addPage(row);
		this.count=this.count+row;
	}
	
	public void setPage(String key, int index, int row){
		int old=getPage(key, index);
		KeyPageModul page=getKeyPage(key);
		page.setPage(index, row);
		this.count=this.count+row-old;
	}
	
	public int getPage(String key, int index){
		KeyPageModul page=(KeyPageModul) this.dict.get(key);
		if(page==null) return 0;
		else {
			return page.getPage(index);
		}
	}
	
	public int getKeyCount(String key){
		KeyPageModul page=getKeyPage(key);
		return page.getCount();
	}
	
	public int getKeyFileCount(String key){
		KeyPageModul page=getKeyPage(key);
		return page.getFileCount();
	}
	
	public int getFileCount(){
		int len=this.dict.size();
		ArrayList<String> list=this.dict.getKeylist();
		int fcount=0;
		for(int i=0; i<len; i++){
			String key=list.get(i);
			fcount=fcount+((KeyPageModul)this.dict.get(key)).getFileCount();
		}
		return fcount;
	}
	
	public int getKeyCount(){
		return this.keycount;
	}
	
	public ArrayList<String> getKeyList(){
		return this.keylist;
	}
	
	public int getKeyStart(String key){
		int len=this.keycount;
		int pos=0;
		for(int i=0; i<len; i++){
			String key1=keylist.get(i);
			if(key1!=null){
				if(!key1.equals(key)){
					pos=pos+getKeyCount(key1);
				} else {
					return pos-1;
				}
			}
		}
		return -2;
	}
	
	public int getKeyEnd(String key){
		int pos=getKeyStart(key);
		if(pos>=-1){
			pos=pos+getKeyCount(key);
		}
		return pos;
	}
	
	public IndexModul getFileIndex(int index){
		IndexModul p=new IndexModul();
		int len=this.keycount;
		int fcount=0;
		int z=0;
		for(int i=0; i<len; i++){
			String key=keylist.get(i);
			z=z+getKeyCount(key);
			fcount=fcount+getKeyFileCount(key);
			if(index<z){
				p.setKey(key);
				break;
			}
		}
		if(p.getKey()!=null){
			KeyPageModul page=getKeyPage(p.getKey());
			int nindex=index-z+getKeyCount(p.getKey());
			fcount=fcount-getKeyFileCount(p.getKey());
			IndexModul zp=page.getFileIndex(nindex);
			//p.setIndex(zp.getIndex()-1);
			//p.setFileIndex(fcount+zp.getFileIndex());
			p.setIndex(zp.getIndex());
			p.setFileIndex(zp.getFileIndex());
		}
		return p;
	}
	
	public IndexModul getFileIndex(String key, int index){
		KeyPageModul page=getKeyPage(key);
		return page.getFileIndex(page.getCount()-1);
	}
	
	public String toString(){
		StringBuilder sb=new StringBuilder();
		sb.append("\rcount: "+getCount());
		sb.append("\rfile count: "+getFileCount());
		int findex=-1;
		for(int i=0; i<this.keycount; i++){
			String key=this.keylist.get(i);
			sb.append("\r"+key);
			int len=getKeyFileCount(key);
			for(int j=0; j<len; j++){
				findex++;
				sb.append("\r序号（"+findex+"） 第"+(j+1)+"页"+getPage(key,j));
			}
		}
		return sb.toString();
	}
	
	public String getFileIndexString(int index){
		IndexModul p=getFileIndex(index);
		StringBuilder sb=new StringBuilder();
		sb.append("\rcount: "+getCount());
		sb.append("\rfile count: "+getFileCount());
		sb.append("\rindex: "+index);
		sb.append("\rkey: "+p.getKey());
		sb.append("\rfileIndex: "+p.getFileIndex());
		sb.append("\rindex: "+p.getIndex());
		return sb.toString();
	}
	
}
